<%@ page import="emp.entity.User" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>添加用户</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f4f4f4;
      margin: 0;
      padding: 0;
    }
    .container {
      width: 100%;
      max-width: 500px;
      margin: 50px auto;
      padding: 20px;
      background-color: #fff;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
      border-radius: 8px;
    }
    h2 {
      text-align: center;
    }
    label {
      display: block;
      margin: 10px 0 5px;
    }
    input[type="text"], input[type="password"], select {
      width: 100%;
      padding: 8px;
      margin: 5px 0 15px;
      border: 1px solid #ccc;
      border-radius: 4px;
    }
    button {
      width: 100%;
      padding: 10px;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 4px;
      font-size: 16px;
      cursor: pointer;
    }
    button:hover {
      background-color: #45a049;
    }
    .message {
      text-align: center;
      font-size: 14px;
      margin-top: 20px;
    }
  </style>
</head>
<body>
<div class="container">
  <%
    // 假设id是从request中获取的，如果有的话
    String id = request.getParameter("id");
    // 获取从 Servlet 传递来的用户信息
    User user = (User) request.getAttribute("user");
  %>
  <h2>添加用户</h2>
  <!-- 表单提交到 /addUser.jsp -->
  <form action="user?<%= id == null ? "action=add" : "action=update&id=" + id %>" method="POST">
    <label for="username">用户名</label>
    <input type="text" id="username" name="username"
           value="<%= (user != null) ? user.getUsername() : "" %>" required>

    <label for="password">密码</label>
    <input type="password" id="password" name="password"
           value="<%= (user != null) ? user.getPassword() : "" %>" required>

    <label for="role">权限</label>
    <select id="role" name="role" required>
      <option value="role" <%= (user != null && user.getRole().equals("role")) ? "selected" : "" %>>Role</option>
      <option value="user" <%= (user != null && user.getRole().equals("user")) ? "selected" : "" %>>User</option>
    </select>


    <button type="submit">
      <%= (id == null || id.isEmpty()) ? "添加用户" : "修改" %>
    </button>

  </form>

  <div class="message">
    <c:if test="${not empty successMessage}">
      <span style="color:green">${successMessage}</span>
    </c:if>
    <c:if test="${not empty errorMessage}">
      <span style="color:red">${errorMessage}</span>
    </c:if>
  </div>
</div>
</body>
</html>

